package com.hubujun.driverlicense.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;

import com.hubujun.driverlicense.R;
import com.hubujun.driverlicense.util.Ln;
import com.hubujun.driverlicense.util.ShowUtils;

import java.util.LinkedList;

public class BaseActivity extends AppCompatActivity {

    private LinkedList<Dialog> mSafeDialogs = new LinkedList<Dialog>();

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        super.startActivityForResult(intent, requestCode, options);
        overridePendingTransition(R.anim.enter_righttoleft, R.anim.exit_righttoleft);
    }


    /**
     * 在当前焦聚的view隐藏输入框
     */
    public void hideSoftKeyboard() {
        if (getCurrentFocus() != null)
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }

    /**
     * 显示 软键盘
     *
     * @param input
     */
    public void showSoftKeyboard(EditText input) {
        if (input != null && input.requestFocus()) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            // 如果输入法打开则关闭，如果没打开则打开
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            imm.showSoftInput(input, InputMethodManager.SHOW_FORCED);
        }
    }

    /**
     * listview正在滚动中隐藏输入框
     *
     * @param listview
     */
    protected void hideSoftKeyboardOnListScroll(ListView listview) {
        listview.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScroll(AbsListView abslistview, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }

            @Override
            public void onScrollStateChanged(AbsListView abslistview, int i) {
                if (i == SCROLL_STATE_TOUCH_SCROLL)
                    hideSoftKeyboard();
            }
        });
    }

    protected void hideSoftKeyboardOnScrollView(ScrollView scrollview) {
        if (scrollview != null) {
            scrollview.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    hideSoftKeyboard();
                    return false;
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle bundle) {
        Ln.d("%s onCreate", getClass().getSimpleName());
        execBeforeSetContentViews();
        super.onCreate(bundle);

        // 使用有256级alpha通道的显示模式
        getWindow().setFormat(PixelFormat.RGBA_8888);
    }

    protected void execBeforeSetContentViews() {

    }


    @Override
    protected void onDestroy() {
        Ln.d("%s onDestroy", getClass().getSimpleName());


        for (Dialog dialog : mSafeDialogs) {
            if (dialog != null)
                dialog.dismiss();
        }

        super.onDestroy();
    }

    @Override
    public void finish() {
        Ln.d("%s finish", getClass().getSimpleName());
        super.finish();
        overridePendingTransition(R.anim.enter_lefttoright, R.anim.exit_lefttoright);
    }

    public void onLocationChanged(Location location) {
    }

    /**
     * 是否跟踪程序前后台情况,子类根据需要重写此方法
     *
     * @return
     */
    public boolean noTrack() {
        return false;
    }

    @Override
    protected void onPause() {
        Ln.d("%s onPause", getClass().getSimpleName());
        super.onPause();
        if (isFinishing()) {
            hideSoftKeyboard();
        }

    }

    @Override
    protected void onResume() {
        Ln.d("%s onResume", getClass().getSimpleName());
        super.onResume();

    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    /**
     * 获得需要保持的数据
     *
     * @Override public final Object onRetainNonConfigurationInstance() { return
     * ConfigurationChangeInjector.saveState(this); }
     */

    @Override
    protected void onStart() {
        Ln.d("%s onStart", getClass().getSimpleName());
        super.onStart();
    }

    @Override
    protected void onStop() {
        Ln.d("%s onStop", getClass().getSimpleName());
        super.onStop();
    }


    public void toastError(String s) {
        ShowUtils.toastMsg(this, s);
    }

    public void toastShortError(String s) {
        ShowUtils.toastShortMsg(this, s);
    }

    public void addDialog(Dialog dialog) {
        mSafeDialogs.add(dialog);
    }

    public void removeDialog(Dialog dialog) {
        mSafeDialogs.remove(dialog);
    }


}
